﻿using System;
using System.Windows.Automation;

namespace QuickUIT.Controls
{
    /// <summary>
    /// Represents a tree item control. 
    /// </summary>
    public class UITreeItem : UIBaseControl
    {
        /// <summary>
        /// Gets the current expand collapse state of the control.
        /// If the control does not support the <see cref="ExpandCollapsePattern"/> , returns a null reference.
        /// </summary>
        public new ExpandCollapseState? ExpandCollapseState
        {
            get { return base.ExpandCollapseState; }
        }

        /// <summary>
        /// Gets whether the current tree item can expand. 
        /// </summary>
        public bool CanExpand
        {
            get
            {
                if (ExpandCollapseState.HasValue &&
                    ExpandCollapseState != System.Windows.Automation.ExpandCollapseState.LeafNode)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Gets whether the current tree item is expanded.
        /// </summary>
        public bool IsExpanded
        {
            get
            {
                if (ExpandCollapseState.HasValue &&
                    ExpandCollapseState == System.Windows.Automation.ExpandCollapseState.Expanded)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Gets whether the current tree item is selected.
        /// </summary>
        public new bool IsSelected
        {
            get { return base.IsSelected; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITreeItem"/> class. 
        /// </summary>
        public UITreeItem()
            : base(ControlType.TreeItem)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITreeItem"/> class.
        /// </summary>
        /// <param name="parent">The parent of this control.</param>
        public UITreeItem(UIBaseControl parent)
            : base(parent, ControlType.TreeItem)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITreeItem"/> class.
        /// </summary>
        /// <param name="locator">The locator used to find the specific control.</param>
        public UITreeItem(string locator)
            : base(null, ControlType.TreeItem, locator)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITreeItem"/> class.
        /// </summary>
        /// <param name="parent">The parent of this control.</param>
        /// <param name="locator">The locator used to find the specific control.</param>
        public UITreeItem(UIBaseControl parent, string locator)
            : base(parent, ControlType.TreeItem, locator)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITreeItem"/> class.
        /// </summary>
        /// <param name="control">The underlying <see cref="AutomationElement"/> of the control.</param>
        public UITreeItem(AutomationElement control) : base(control, ControlType.TreeItem)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITreeItem"/> class.
        /// </summary>
        /// <param name="parent">The parent of the control</param>
        /// <param name="control">The underlying <see cref="AutomationElement"/> of the control.</param>
        public UITreeItem(UIBaseControl parent, AutomationElement control) : base(parent, control, ControlType.TreeItem)
        {
        }

        /// <summary>
        /// Scrolls the list item into view. 
        /// </summary>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new void ScrollIntoView()
        {
            base.ScrollIntoView();
        }

        /// <summary>
        /// Selects the current list item. 
        /// </summary>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new void Select()
        {
            base.ScrollIntoView();
            base.Select();
        }

        /// <summary>
        /// Adds the list item to the collection of selected items. 
        /// </summary>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new void AddToSelection()
        {
            base.AddToSelection();
        }

        /// <summary>
        /// Removes the list item from the current list selection.
        /// </summary>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new void RemoveFromSelection()
        {
            base.RemoveFromSelection();
        }

        /// <summary>
        /// Expands the current tree item. 
        /// </summary>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new void Expand()
        {
            base.Expand(); 
        }

        /// <summary>
        /// Collapses the current tree item. 
        /// </summary>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new void Collapse()
        {
            base.Collapse(); 
        }

        /// <summary>
        /// Invokes the current tree item if it can perform a command. 
        /// </summary>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        public new void Invoke()
        {
            base.Invoke();
        }

        /// <summary>
        /// Gets the parent selection container of a control. 
        /// </summary>
        /// <returns>The <see cref="AutomationElement"/> of the parent control that implements the <see cref="SelectionPattern"/>.</returns>
        /// <exception cref="InvalidOperationException">The control pattern cannot be performed by the element.</exception>
        /// <exception cref="ElementNotAvailableException">The element is not available anymore.</exception>
        /// <returns>The <see cref="AutomationElement"/> of the selection container.</returns>
        public new AutomationElement GetSelectionContainer()
        {
            return base.GetSelectionContainer();
        }
    }
}
